package com.cqut.forum.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.PictureGroup;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.ControlService;
import com.cqut.forum.service.GardenService;
import com.cqut.forum.service.ModuleService;
import com.cqut.forum.service.PictureGroupService;
import com.cqut.forum.service.PictureService;
import com.cqut.forum.service.WallService;

/**
 * forum管理action
 * @author yangxun
 */
@Controller
public class ControlAction {
	
	@Resource
	private ControlService controlService;
	@Resource
	private PictureGroupService pgService;
	@Resource
	private PictureService pictureService;
	@Resource
	private WallService wallService;
	@Resource
	private GardenService gardenService;
	@Resource
	private ModuleService moduleService;
	@RequestMapping("/control/user_control")
	@ResponseBody
	public List<Map<String, Object>> loadUser(HttpServletRequest request,
			String userName,String userPlace,String registerTime,int index,int size){
		List<Map<String, Object>> users = controlService.loadUser(userName, userPlace, registerTime, index, size);
		
		return users;
	}
	@RequestMapping("/control/user_control_change")
	@ResponseBody
	public List<Map<String, Object>> userControlChange(HttpServletRequest request,
			Integer[] userIds,String[] roleNames){
		List<Integer> users = java.util.Arrays.asList(userIds);
		List<String> roles = java.util.Arrays.asList(roleNames);

		controlService.updateUserRole(users, roles);
		return null;
	}
	
	@RequestMapping("/control/article_control")
	@ResponseBody
	public List<Map<String, Object>> loadArticle(HttpServletRequest request,int index,int size,
			String articleOwner,String articleTitle,String publishTime){
		List<Map<String, Object>> articles = controlService.loadArticle(articleOwner,articleTitle,publishTime,index,size);
		
		return articles;
	}
	
	@RequestMapping("/control/article_size")
	@ResponseBody
	public int countSize(String moduleName){
		int size = controlService.countSize(moduleName);
		return size;
	}
	
	@RequestMapping("/control/article_control_change")
	@ResponseBody
	public List<Map<String, Object>> loadArticleChange(HttpServletRequest request,
			Integer[] articles,String[] states){
		User user = (User)request.getSession().getAttribute("user");
		List<Integer> a = java.util.Arrays.asList(articles);
		List<String> s = java.util.Arrays.asList(states);

		controlService.updateArticleState(user.getUserId(),a,s);
		return null;
	}
	
	@RequestMapping("/control/picture_group_change")
	@ResponseBody
	public String createPictureGroup(HttpServletRequest request,
			String[] groupNames,int[] groupIds,boolean[] states){
		User user = (User)request.getSession().getAttribute("user");
		for(int i =0;i<groupNames.length;i++){
			/*PictureGroup pg = new PictureGroup();
			pg.setIsPublic(true);
			pg.setPictureGroupName(name);
			pg.setPictureNumber(0);
			pg.setOwnerId(((User)request.getSession().getAttribute("user")).getUserId());
			pg.setPicturePassword("");*/
			if(groupIds[i]>0){
				pgService.updateGroupName(groupIds[i], groupNames[i],states[i]);
			}
			else{
				PictureGroup pg = new PictureGroup();
				pg.setIsPublic(true);
				pg.setMarkPictureUrl("");
				pg.setOwnerId(user.getUserId());
				pg.setPictureGroupName(groupNames[i]);
				pg.setPictureNumber(0);
				pg.setPicturePassword("");
				pgService.insert(pg);
			}
		}
		
		return "创建成功";
	}
	@RequestMapping("/control/picture_group_control")
	@ResponseBody
	public List<Map<String, Object>> loadPictureGroup(HttpServletRequest request){
		List<Map<String, Object>> pgs = pgService.getSysteGroups();
		return pgs;
	}
	@RequestMapping("/control/picture_control")
	@ResponseBody
	public List<Map<String, Object>> loadPicture(HttpServletRequest request,int index,int size,
			String pictureOwner,String pictureType,String uploadTime){
		
		List<Map<String, Object>> pictures = pictureService.loadPicture(pictureOwner, pictureType, uploadTime,index,size);
		return pictures;
	}

	@RequestMapping("/control/picture_control_delete")
	@ResponseBody
	public String deletePicture(HttpServletRequest request,int[] pictureId){
		for(int id:pictureId){			
			//pictureService.deleteById(id);
			pictureService.updatePictureState(id, false);
		}
		return "删除成功!";
	}
	@RequestMapping("/control/wall_control")
	@ResponseBody
	public List<Map<String, Object>> loadWall(HttpServletRequest request,int index,int size,
			String wallOwner,String wallName,String publishTime){
		List<Map<String, Object>> walls = wallService.loadWall(wallOwner,wallName,publishTime,index,size);
		return walls;
	}
	@RequestMapping("/control/wall_control_change")
	@ResponseBody
	public String wallChange(HttpServletRequest request,
			Integer[] wallIds,String[] states){
		List<Integer> w = java.util.Arrays.asList(wallIds);
		List<String> s = java.util.Arrays.asList(states);
		controlService.updateWallState(w, s);
		return "修改成功!";
	}
	@RequestMapping("/control/garden_control")
	@ResponseBody
	public List<Map<String, Object>> loadGarden(HttpServletRequest request,int index,int size,
			String userName,String gardenType,String publishTime){
		List<Map<String, Object>> gardens = gardenService.loadGarden(userName,gardenType,publishTime,index,size);
		return gardens;
	}
	@RequestMapping("/control/garden_control_change")
	@ResponseBody
	public String gardenChange(HttpServletRequest request,
			Integer[] gardenIds,String[] states){
		User user = (User)request.getSession().getAttribute("user");
		List<Integer> g = java.util.Arrays.asList(gardenIds);
		List<String> s = java.util.Arrays.asList(states);
		controlService.updateArticleState(user.getUserId(),g, s);
		return "修改成功!";
	}
	
	@RequestMapping("/control/back_control")
	@ResponseBody
	public Map<String, Object> loadModuleInfo(HttpServletRequest request){
		List<Map<String, Object>> modules = moduleService.loadModules();
		List<Map<String, Object>> moduleInfos = moduleService.loadModuleInfos();
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("modules", modules);
		result.put("moduleInfos", moduleInfos);
		return result;
	}
	@SuppressWarnings("unchecked")
	@RequestMapping("/control/back_control_change")
	@ResponseBody
	public String moduleInfoChange(HttpServletRequest request,
			Integer[] moduleIds,String[] csss){
		moduleService.updateModuleCss(moduleIds,csss);
		List<Map<String, Object>> ss = (List<Map<String, Object>>) request.getSession().getServletContext().getAttribute("ss");
		if(ss!=null){
			for(int i =0;i<moduleIds.length;i++){
				int id = moduleIds[i];
				for(int j=0;j<ss.size();j++){
					Map<String, Object> s = ss.remove(j);
					if(id == (Integer)s.get("moduleId")){
						s.put("moduleCss", csss[i]);
						break;
					}
					ss.add(s);
				}
			}
		}
		return "修改成功！";
	}
}






